package com.glp.work.flow.utils;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 数据转换工具类
 *
 * @author xhshen
 */
@Slf4j
public class DataUtils {

    public static <P, D> PageInfo<D> pageInfo2PageInfoDTO(PageInfo<P> pageInfo, Class<D> dClass) {
        Page<D> page = new Page<>(pageInfo.getPageNum(), pageInfo.getPageSize());
        page.setTotal(pageInfo.getTotal());
        page.setPageNum(pageInfo.getPageNum());
        page.setPageSize(pageInfo.getPageSize());
        for (P p : pageInfo.getList()) {
            try {
                D d = dClass.newInstance();
                BeanUtils.copyProperties(p, d);
                page.add(d);
            } catch (InstantiationException e) {
                log.error("DataUtils method pageInfo2PageInfoDTO InstantiationException!", e);
            } catch (IllegalAccessException e) {
                log.error("DataUtils method pageInfo2PageInfoDTO IllegalAccessException!", e);
            }
        }
        return new PageInfo<>(page);
    }


    public static <S, T> List<T> list2List(List<S> sourceList, Class<T> targetClass) {
        List<T> targetList = new ArrayList<>();
        if (CollectionUtils.isEmpty(sourceList)) {
            return targetList;
        }
        for (S s : sourceList) {
            try {
                T t = targetClass.newInstance();
                BeanUtils.copyProperties(s, t);
                targetList.add(t);
            } catch (Exception e) {
                log.error("DataUtils method list2ListDTO Exception!", e);
            }
        }
        return targetList;
    }


    public static <T> List<T> copyList(List list, Class<T> tClass) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return JSON.parseArray(JSON.toJSONString(list), tClass);
    }


    public static Map<String, Object> copyMap(Map map) {
        return JSON.parseObject(JSON.toJSONString(map));
    }

    /**
     * 拷贝属性  忽略null值
     *
     * @param src    源对象
     * @param target 目标对象
     */
    public static void copyPropertiesIgnoreNull(Object src, Object target) {
        BeanUtils.copyProperties(src, target, getNullPropertyNames(src));
    }

    private static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<>();
        for (java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
}